import mongoose, { Document, Schema } from 'mongoose';
import { IRepairRecord } from '@/types';

export interface IRepairDocument extends Omit<IRepairRecord, '_id'>, Document {}

const repairSchema = new Schema<IRepairDocument>(
  {
    userId: {
      type: String,
      required: [true, '用户ID是必需的'],
    },
    vehicleId: {
      type: String,
      required: [true, '车辆ID是必需的'],
    },
    date: {
      type: Date,
      required: [true, '维修日期是必需的'],
      default: Date.now,
    },
    odometer: {
      type: Number,
      required: [true, '里程表是必需的'],
      min: [0, '里程表不能为负数'],
    },
    type: {
      type: String,
      required: [true, '维修类型是必需的'],
      trim: true,
      maxlength: [100, '维修类型不能超过100个字符'],
    },
    laborCost: {
      type: Number,
      required: [true, '工时费是必需的'],
      min: [0, '工时费不能为负数'],
    },
    partsCost: {
      type: Number,
      required: [true, '配件费是必需的'],
      min: [0, '配件费不能为负数'],
    },
    totalCost: {
      type: Number,
      required: [true, '总费用是必需的'],
      min: [0, '总费用不能为负数'],
    },
    description: {
      type: String,
      trim: true,
      maxlength: [500, '描述不能超过500个字符'],
    },
  },
  {
    timestamps: true,
  }
);

// 索引
repairSchema.index({ userId: 1 });
repairSchema.index({ vehicleId: 1 });
repairSchema.index({ type: 1 });
repairSchema.index({ date: 1 });
repairSchema.index({ userId: 1, vehicleId: 1 });
repairSchema.index({ userId: 1, date: 1 });

export const Repair = mongoose.model<IRepairDocument>('Repair', repairSchema);
